package com.shujia.zookeeper;

import org.apache.zookeeper.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Code03GetNodeData {
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {

        int sessionTimeout = 5 * 1000;
        ZooKeeper zkCli = new ZooKeeper("master:2181,node1:2181,node2:2181", sessionTimeout, new Watcher() {
            // 处理监视结果
            @Override
            public void process(WatchedEvent event) {
                System.out.println("监视器被打开... 正在连接...");
            }
        });
        /**
         *
         path – the given path
         watcher – explicit watcher
         stat – the stat of the node
         public byte[] getData(final String path, Watcher watcher, Stat stat)
         */
        String path = "/api";
        byte[] data = zkCli.getData(path
                ,
                new Watcher() {
                    @Override
                    public void process(WatchedEvent event) {
                        System.out.println("正在监听..");
                        System.out.println(event.getType());
                    }
                }, zkCli.exists(path, new Watcher() {
                    @Override
                    public void process(WatchedEvent event) {
                        System.out.println("节点信息被监听...");
                    }
                })
        );
        System.out.println("data:"+new String(data));
        zkCli.close();
    }
}
